// pages/module/orderDetail/orderDetail.js
import {
	checkXpOrder,
	retXpOrder,
	cancelXpOrder,
	xpOrderDetail,
	orderPay,
	orderConfirm
} from '../../../utils/imapi.js';
import Dialog from '../../common/components/vant/dialog/dialog';

import {
	kuaidi_search
} from '../../../utils/util'
var app = getApp();
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		orderInfo: {
			status: '待支付',
			order_status: 0,
			date: '23小时55分钟后自动确认',
			buy_user_name: '朱文君',
			buy_user_mobile: '86-18844994657',
			buy_user_address: '江苏省苏州市沧浪区 沧浪区盘溪新村51幢602室'
		},
		order_no: "",
		status_list: [{
				status: "待付款",
				status_id: '0'
			},
			{
				status: "待发货",
				status_id: '1'
			},
			{
				status: "待收货",
				status_id: '2'
			},
			{
				status: "交易成功",
				status_id: '3'
			},
			{
				status: "退款中",
				status_id: '4'
			},
			{
				status: "已退款",
				status_id: '5'
			},
		],
		show_part: false,
		selectList: [{
				name: '价格有点贵',
				is_select: 1
			},
			{
				name: '规格/款式/数量拍错',
				is_select: 0
			},
			{
				name: '收货地址拍错',
				is_select: 0
			},
			{
				name: '暂时不需要了',
				is_select: 0
			},
			{
				name: '其他',
				is_select: 0
			}
		],
		cancelId: '',
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: async function(options) {
		this.setData({
			order_no: options.id,
		})
	},
	onShow: function() {
		this.getData();
	},
	async getData() {
		const {
			data
		} = await xpOrderDetail({
			order_id: this.data.order_no
		})
		this.setData({
			orderInfo: {
				...data,
			},
		})
	},
	copy: function(e) {
		wx.setClipboardData({
			data: e.target.dataset.text + "",
			success: function(res) {
				console.log(res)
			},
			fail: function(err) {
				console.log(err)
			}
		});
	},
	copy1() {
		wx.setClipboardData({
			data: this.data.orderInfo.express_no,
			success: function(res) {
				 wx.showToast({	
				     title: '复制成功',	
				     icon: 'success',
				     duration: 1000
				   })
			}
		})
	},
	logistics() {
		wx.navigateTo({
			url: '/pages/module/logistics/logistics',
		})
	},
	cancleSure() {
		let self = this;
		Dialog.confirm({
				title: '提示',
				message: '是否确认取消订单',
			})
			.then(() => {
				let reason_info = self.data.selectList.filter(item => item.is_select == 1)[0]
				cancelXpOrder({
					order_no: self.data.orderInfo.order_id,
					reason_info: reason_info.name
				}).then(res => {
					self.getData();
					self.hideShare();
				})
			})
			.catch(() => {

			});
	},
	cancle(e) {
		console.log(e);
		this.setData({
			show_part: true,
		})
	},

	hideShare() {
		this.setData({
			show_part: false,
		})
	},
	select(e) {
		var index = e.currentTarget.dataset.index;
		this.data.selectList.forEach(item => {
			item.is_select = 0
		})
		this.data.selectList[index].is_select = 1
		this.setData({
			selectList: this.data.selectList
		})
	},
	async pay(e) {
		let self = this;
		let orderInfo = this.data.orderInfo;
		const data = {
			price: orderInfo.act_price * orderInfo.goods_amt,
			in_out: 'I',
			fin_type: 'GW',
			ref_order_no: orderInfo.order_id.toString(),
		}
		const res = await orderPay(data);
		if (res.code === 0) {
			wx.requestPayment({
				timeStamp: res.data.timestamp,
				nonceStr: res.data.nonceStr,
				package: res.data.package,
				signType: res.data.signType,
				paySign: res.data.paySign,
				success: () => {
					wx.navigateBack();
				},
				fail: (err) => {
					wx.navigateBack();
					console.log('失败', err)
				}
			})
		}
	},
	refund() {
		let self = this;
		wx.navigateTo({
			url: '/pages/module/refund/refund',
			success: function(res) {
				res.eventChannel.emit('acceptDataFromOpenerPage', {
					id: self.data.orderInfo.order_id
				})
			}
		})
	},
	viewWuliu() {
		// wx.navigateTo({
		//   url: '/pages/module/logistics/logistics',
		// })
		if(this.data.orderInfo.express_no){
			kuaidi_search(this.data.orderInfo.express_no)

		}

		
	},
	//确认收货
	sure() {
		let self = this;
		let orderInfo = this.data.orderInfo;

		console.log('===== order:',orderInfo)
		Dialog.confirm({
				title: '提示',
				message: '是否确认收货',
			})
			.then(() => {
				orderConfirm({
					order_id: orderInfo.order_id.toString()
				}).then(res => {
					wx.showToast({
					  title: '收货成功!',
					})
					self.getData();
				})
			})
			.catch(() => {

			});
	}
})
